package game;

import action.Action;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public abstract class ResolutionController {
    public static final int TICKS_PER_RESOLUTION = 25;
    private static int tickNum = 1;
    private static boolean paused = false;
    private static boolean listenersQueuedToPause = false;
    private static List<ResolutionListener> resolutionListeners = new ArrayList<ResolutionListener>();
    private static List<Action> actionsToComplete = new LinkedList<Action>();
    
    public static void tick() {
        if(!paused) {
            if(tickNum <= TICKS_PER_RESOLUTION) {
                System.out.println("tickNum: "+tickNum);
                for(ResolutionListener resolutionListener : getApplicableListeners()) {
                    resolutionListener.tick();
                }
                for(Action action : actionsToComplete) {
                    action.completeAction();
                }
                actionsToComplete.clear(); //don't remove items from a list while you are iterating through it!
                tickNum++;
            } else {
                endResolution();
            }
        } else if (listenersQueuedToPause) {
            for(ResolutionListener listener : getApplicableListeners()) {
                listener.resolutionPaused();
            }
            listenersQueuedToPause = false;
        }
    }
    
    public static void startResolution() {
        tickNum = 1;
        for(ResolutionListener resolutionListener : getApplicableListeners()) {
            resolutionListener.resolutionStarted();
        }
    }
    
    protected static void endResolution() {
        for(ResolutionListener resolutionListener : getApplicableListeners()) {
            resolutionListener.resolutionEnded();
        }
        StateController.resolutionEnded();
    }
    
    /* Toggles resolution pause state. If pausing, queues all listeners to pause at the end of the current tick. */
    public static void toggleResolutionPaused() {
        if(paused) {
            paused = false;
            for(ResolutionListener listener : getApplicableListeners()) {
                listener.resolutionUnpaused();
            }
        } else {
            paused = true;
            listenersQueuedToPause = true;
        }
    }
    
    /** Returns the current listeners which should tick or be notified of updates. */
    private static List<ResolutionListener> getApplicableListeners() {
        List<ResolutionListener> applicableListeners = new ArrayList<ResolutionListener>();
        for(ResolutionListener resolutionListener : resolutionListeners) {
            if(TurnController.takeTurnsResolving && resolutionListener.getPlayer().equals(TurnController.getCurrentPlayer()))
                applicableListeners.add(resolutionListener);
        }
        return applicableListeners;
    }
    
    public static void registerResolutionListener(ResolutionListener resolutionListener) {
        resolutionListeners.add(resolutionListener);
    }

    /** Queues an action to be completed at the end of the current tick. */
    public static void queueForCompletion(Action action) {
        actionsToComplete.add(action);
    }

    public static int getTicksRemaining() {
        return TICKS_PER_RESOLUTION - tickNum + 1;
    }
}
